﻿using System;
using System.Linq;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;

namespace DataProcessor.Process
{
    public static class DataInstructionProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DataInstructionProcessor));
        //实时数据指令
        public static void Process2011(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrWhiteSpace(mn)) return;
                var port = Util.GetInfoFromMn(mn);
                if (port == null)
                {
                    Log.Warn("MN：" + mn + " 找不到对应企业排口！");
                    return;
                }
                var dataTime = Parser.GetDateTimeByKey("DataTime", content);
                if (dataTime == null)
                {
                    Log.Warn("指令中没有时间！");
                    return;
                }
                var dataTimeValue = dataTime.Value;
                var pollutantCodes = Parser.GetPollutantCodes(content);

                using (var contextTarget = new PlatformPhContext())
                {
                    //todo:性能会有问题。
                    var master = contextTarget.data_realtime_main.Where(t => t.ps_code.Equals(port.ps_code)).Where(t => t.pi_code.Equals(port.code))
                        .FirstOrDefault(t => t.monitor_time == dataTimeValue);

                    if (master == null)
                    {
                        master = new data_realtime_main
                        {
                            monitor_time = dataTimeValue,
                            is_synchronize = false,
                            ps_code = port.ps_code,
                            pi_code = port.code,
                            type = "ph",
                            day_index=int.Parse(dataTimeValue.ToString("yyyyMMdd"))
                        };
                        contextTarget.data_realtime_main.Add(master);
                        contextTarget.SaveChanges();
                    }
                    foreach (var detailCode in pollutantCodes)
                    {
                        var factor = Util.GetFactor(detailCode);
                        if (factor == null)
                        {
                            Log.Warn("Factor：" + detailCode + " 找不到污染物因子！");
                            continue;
                        }
                        var rtdDouble = Parser.GetFloatByKey(detailCode + "-Rtd", content);
                        if (rtdDouble == null)
                        {
                            Log.Warn("Rtd：没有RTD！");
                            continue;
                        }

                        var detail = contextTarget.data_realtime.Where(t => t.main_id == master.id)
                            .FirstOrDefault(t => t.pollutant_code.Equals(detailCode));
                        if (detail == null)
                        {
                            detail = new data_realtime();
                            contextTarget.data_realtime.Add(detail);
                        }
                        detail.main_id = master.id;
                        detail.Cou = rtdDouble.Value;
                        detail.Min = rtdDouble.Value;
                        detail.Avg = rtdDouble.Value;
                        detail.Max = rtdDouble.Value;
                        detail.pollutant_code = factor.code;

                    }
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                if (ex.InnerException != null)
                {
                    Log.Error(ex.InnerException);
                }
                Log.Error(content);
            }
        }
       
    }
}
